//
//  LCHomeCellViewModel.swift
//  SinaWeiBo
//
//  Created by mac on 16/12/5.
//  Copyright © 2016年 heima. All rights reserved.
//

import UIKit

class LCHomeCellViewModel: NSObject {
    //需要拿到属性数据
    var statusess:LCHomeStatusesModel?{
        didSet{
            guard let currentStatus = statusess else {
                return
            }
            //  处理会员等级图片
            handlembrankImage(mbrank: currentStatus.user?.mbrank ?? 0)
            //  处理认证类型等级图片
            handleVerifiedTypeImage(verfiedType: currentStatus.user?.verified_type ?? 0)
            
            retweetCountContent = handleCountContent(count: currentStatus.reposts_count, defaultTitle: "转发")
            commentCountContent = handleCountContent(count: currentStatus.comments_count, defaultTitle: "评论")
            unlikeCountContent = handleCountContent(count: currentStatus.attitudes_count, defaultTitle: "点赞")
            
            //处理转发内容
            handleReweetedMessage()
            
            //处理来源数据
            handleSourceContent(source: statusess?.source ?? "")
            
        }
        
        
    }
//MARK:转发内容处理(前面加@name)
    var contentMessage: String?
    
//MARK:会员等级和认证类型
    //  会员等级图片
    var mbrankImage: UIImage?
    //  认证类型等级图片
    var verifiedTypeImage: UIImage?
    
    
//MARK:toolBar数量
    //  转发数内容
    var retweetCountContent: String?
    //  评论数内容
    var commentCountContent: String?
    //  赞数内容
    var unlikeCountContent: String?
    
    var contentSource: String?
    
    
    //处理来源数据
    private func handleSourceContent(source:String){
        if source.contains("\">") && source.contains("</") {
            if let startRange = source.range(of: "\">"),let endRange = source.range(of: "</"){
                let startIndex = startRange.upperBound
                let endIndex = endRange.lowerBound
                
                let result = source.substring(with: startIndex..<endIndex)
                
                contentSource = "来自: " + result
            }
        }
    }
    
    //解决转发内容的方法
    private func handleReweetedMessage(){
        if let message = statusess?.retweeted_status?.text{
            
            contentMessage = "@\(statusess?.retweeted_status?.user?.name):\(message)"
        }
        
    }
    /*  转发,评论,赞的数据显示逻辑
     1. 判断数量是否大于0 如果大于0 判断其是否大于等10000, 如果大于等于10000,显示1.x万
     2. 如果大于0 小于10000,那么显示指定数据
     3. 如果等于0 那么显示原始文字
     */
    
    private func handleCountContent(count: Int, defaultTitle: String) -> String {
        
        if count > 0 {
            if count >= 10000 {
                let result = CGFloat(count / 1000) / 10
                
                let str = "\(result)万"
                if str.contains(".0") {
                    //  替换特定字符串
                    return str.replacingOccurrences(of: ".0", with: "")
                } else {
                    return str
                }

            } else {
                return "\(count)"
            }
        } else {
            //  显示原始文字
            return defaultTitle
        }
    }
    
    //  处理认证类型等级图片
    private func handleVerifiedTypeImage(verfiedType: Int) {
        /// 认证类型 -1：没有认证，1，认证用户，2,3,5: 企业认证，220: 达人
        switch verfiedType {
        case 1:
            verifiedTypeImage = UIImage(named: "avatar_vip")
        case 2, 3, 5:
            verifiedTypeImage = UIImage(named: "avatar_enterprise_vip")
        case 220:
            verifiedTypeImage = UIImage(named: "avatar_grassroot")
        default:
            verifiedTypeImage = nil
        }
        
    }
    
    
    //  处理会员等级图片
    private func handlembrankImage(mbrank: Int) {
        //  会员等级 1-6
        if mbrank >= 1 && mbrank <= 6 {
            mbrankImage = UIImage(named: "common_icon_membership_level\(mbrank)")
        }
        
        
    }
    
}
